package com.situ.water_read.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.water_read.mapper.RdReadMapper;
import com.situ.water_read.mapper.RdTaskMapper;
import com.situ.water_read.pojo.RdRead;
import com.situ.water_read.pojo.RdTask;
import com.situ.water_read.pojo.RdYearmonth;
import com.situ.water_read.mapper.RdYearmonthMapper;
import com.situ.water_read.service.RdYearmonthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.water_user.mapper.UsUserMapper;
import com.situ.water_user.pojo.UsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.YearMonth;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@Service
public class RdYearmonthServiceImpl extends ServiceImpl<RdYearmonthMapper, RdYearmonth> implements RdYearmonthService {
    @Autowired
    private RdYearmonthMapper yearmapper;
    @Autowired
    UsUserMapper userMapper;
    @Autowired
    RdReadMapper readMapper;
    @Autowired
    RdTaskMapper taskMapper;
    @Override
    @Transactional
    public void addYearMouth(String str,Integer empid) {
        if(empid==null){
            empid=6;
        }
        Date date = new Date();
        RdYearmonth yearmonth=new RdYearmonth();
        String[] split = str.split("年");
        String year=split[0];
        String month=split[1].substring(0,split[1].length()-1);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("ReadYear",year);
        wrapper.eq("ReadMonth",month);

            //判断表里是否有数据
            List<RdRead> alllist = readMapper.selectList(null);
            if(alllist.size()>0&&alllist!=null){//表里有数据
                System.out.println("表里有数据");
                List list = yearmapper.selectList(wrapper);
                System.out.println(list);
                //判断本月是否有初始化
                if(list.size()==0||list==null) {
                    System.out.println("本月没有初始化");
                    if (month == "1") {//当当前月份为一月时
                        yearmonth.setReadMonth(12);
                        yearmonth.setReadYear(Integer.parseInt(year) - 1);
                    } else {
                        yearmonth.setReadMonth(Integer.parseInt(month) - 1);
                        yearmonth.setReadYear(Integer.parseInt(year));
                    }
                    RdYearmonth lastyearmonth = yearmapper.queryyear(yearmonth);
                    //修改上月的当前布尔值，初始化结束时间
                    lastyearmonth.setIsCurrent(false);
                    lastyearmonth.setEndDate(date);
                    yearmapper.updateById(lastyearmonth);
                    //添加本月的年月日，当前true，初始人，初始日期
                    RdYearmonth Nowyearmonth = new RdYearmonth();
                    Nowyearmonth.setReadYear(Integer.valueOf(year));
                    Nowyearmonth.setReadMonth(Integer.valueOf(month));
                    Nowyearmonth.setIsCurrent(true);
                    Nowyearmonth.setInitEmpID(empid);//通过session取
                    Nowyearmonth.setInitDate(date);
                    yearmapper.insert(Nowyearmonth);
                    //初始化抄表数据
                    this.addRead(Integer.parseInt(year), Integer.parseInt(month), date);
                    this.addRdTask(Integer.parseInt(year), Integer.parseInt(month));
                }else {
                    System.out.println("本月已初始化");
                    List<RdRead> list1 = readMapper.selectList(wrapper);
                    for (RdRead read : list1) {
                        readMapper.updateAudit(read.getId());
                    }
                }
        }else{//表里没有数据，直接添加当前
                System.out.println("表里没有数据，直接添加当前");
                yearmonth.setReadYear(Integer.parseInt(year));
                yearmonth.setReadMonth(Integer.parseInt(month));
                yearmonth.setIsCurrent(true);
                yearmonth.setId(empid);
                yearmonth.setInitDate(date);
                this.addRead(Integer.parseInt(year), Integer.parseInt(month), date);
                this.addRdTask(Integer.parseInt(year), Integer.parseInt(month));
            }
    }

    @Override
    public List<RdYearmonth> queryYearmonth() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("ReadYear","ReadMonth");
        return yearmapper.selectList(wrapper);
    }

    @Override
    public List<Map> queryall() {

        return yearmapper.selectall();
    }

    /**
     * 每月初始化抄表数据表中的用户数据(用户为可使用，)
     */
    @Transactional
    public void addRead(Integer year,Integer month,Date date){
        System.out.println("addread");
        RdRead read=new RdRead();
        read.setReadYear(year);
        read.setReadMonth(month);
        read.setReadDate(date);

        List<Map> list=userMapper.selectuser();
        //查询满足上月已有抄表数据条件的用户
        QueryWrapper wrapper=new QueryWrapper();
        RdRead rdEmpId=new RdRead();//查询抄表人的对象
        if(month==1){
            month=12;
            year-=year;
            wrapper.eq("ReadMonth",month);
            wrapper.eq("ReadYear",year);
        }else{
            month-=1;
            wrapper.eq("ReadYear",year);
            wrapper.eq("ReadMonth",month);
        }
        rdEmpId.setReadYear(year);
        rdEmpId.setReadMonth(month);
        for (Map map:list){
            read.setUserNo((String)map.get("UserNo"));
            read.setMeterNo(map.get("MeterID").toString());
            read.setMaxValue((Integer)map.get("MaxValue"));
            read.setVolumeId((Integer)map.get("VolumeID"));
            read.setVolumeIndex(((Integer)map.get("VolumeOrderIndex")));
            //查询上个月抄表人Id
            rdEmpId.setVolumeId(read.getVolumeId());
            Integer empid=readMapper.selectEmpId(rdEmpId);
            if(empid!=null){
                read.setReadEmpID(empid);
            }
            read.setFormula((String)map.get("Formula"));
            wrapper.eq("UserNo",(String)map.get("userno"));
            List<RdRead> list1 = readMapper.selectList(wrapper);
            if (list1.size()>0||list1!=null){
                read.setPreValue((Integer)list.get(0).get("preValue"));
            }
            int i = readMapper.insert(read);
        }

    }
    /**
     * 修改任务分配表，将上月抄表人，表册复制修改年月
     */
    @Transactional
    public void addRdTask(Integer year,Integer month){
        System.out.println("addRdTask");
        QueryWrapper wrapper=new QueryWrapper();
        if(month==1){
            wrapper.eq("ReadMonth",12);
            wrapper.eq("ReadYear",year-1);
        }else{
            wrapper.eq("ReadYear",year);
            wrapper.eq("ReadMonth",month-1);
        }
        List<RdTask> list = taskMapper.selectList(wrapper);
        if(list.size()>0) {
            RdTask task = new RdTask();
            for (RdTask tasklist : list) {
                task.setReadMonth(month);
                task.setReadYear(year);
                task.setEmpId(tasklist.getEmpId());
                task.setVolumeId(tasklist.getVolumeId());
                taskMapper.insert(task);
            }
        }
    }

}
